from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import SPU,Good
from .serializers import GoodSer,SpuSer
from group.models import Cate
# Create your views here.
class Sku(APIView):
    def get(self,request):
        data = Good.objects.all()
        ser = GoodSer(data,many=True)
        return Response(ser.data)

class Spu(APIView):
    def get(self,request):
        data = SPU.objects.all()
        ser = SpuSer(data,many=True)
        return Response(ser.data)

class GetOneSku(APIView):
    def get(self,request,id):
        try:
            data = Good.objects.get(id=id)
            ser = GoodSer(data)
            return Response(ser.data)
        except Exception as e:
            print(e)
            return Response({'msg':'数据不存在','code':204})
    def put(self,request,id):
        name = request.data.get('name')
        spu_name = request.data.get('spu')
        spu = SPU.objects.get(name=spu_name)
        describe = request.data.get('describe')
        cate_name = request.data.get('cate')
        cate = Cate.objects.get(name=cate_name)
        price = request.data.get('price')
        stock = request.data.get('stock')
        is_selling = request.data.get('is_selling')
        try:
            data = Good.objects.get(id=id)
            data.name = name
            data.spu = spu
            data.describe = describe
            data.cate = cate
            data.price = price
            data.stock = stock
            data.is_selling = is_selling
            data.save()
            return Response({'msg':'修改成功'})
        except Exception as e:
            print(e)
            return Response({'msg':'修改失败','code':204})
    def delete(self,request,id):
        try:
            Good.objects.get(id=id).delete()
            return Response({'msg': '删除成功', 'code': 200})
        except Exception as e:
            print(e)
            return Response({'msg':'数据不存在','code':204})


class AllSku(APIView):
    def get(self,request):
        page = int(request.query_params.get('page'))
        size = int(request.query_params.get('size'))
        data = Good.objects.all()[((page-1)*size):(page*size)]
        ser = GoodSer(data,many=True)
        return Response({'data':ser.data,'count':Good.objects.all().count()})

class AddSku(APIView):
    def post(self,request):
        name = request.data.get('name')
        spu = request.data.get('spu')
        describe = request.data.get('describe')
        cate = request.data.get('cate')
        price = request.data.get('price')
        stock = request.data.get('stock')
        is_selling = request.data.get('is_selling')
        try:
            Good.objects.get(name=name)
            return Response({'msg': '商品已存在', 'code': 204})
        except Exception as e:
            Good.objects.create(
                name=name,
                spu_id=spu,
                describe=describe,
                cate_id=cate,
                price=price,
                stock=stock,
                is_selling=is_selling
            )
            return Response({'msg': '添加成功', 'code': 200})